package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Canbo;
import model.bean.ChuyenAn;
import model.bean.VuAn;
import model.dao.CanBoDAO;
import model.dao.ChuyenAnDAO;
import model.dao.VuAnDAO;

/**
 * Servlet implementation class VuAnServlet
 */
@WebServlet("/VuAnServlet")
public class VuAnServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VuAnServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		CanBoDAO canBoDAO = new CanBoDAO();
		ChuyenAnDAO chuyenAnDAO = new ChuyenAnDAO();
		VuAnDAO vuAnDAO = new VuAnDAO();

		if (request.getParameter("new") != null) {

			ArrayList<Canbo> dsCanBo = canBoDAO.dsCanbo();
			ArrayList<ChuyenAn> dsChuyenAn = chuyenAnDAO.dsChuyenan();
			
			request.setAttribute("dsCanBo", dsCanBo);
			request.setAttribute("dsChuyenAn", dsChuyenAn);

			RequestDispatcher rd = request
					.getRequestDispatcher("form_vuan.jsp");
			rd.forward(request, response);

		}

		//Thêm mới
		else if (request.getParameter("tenvuan") != null) {

			String tenvuan = request.getParameter("tenvuan");
			String idcb = request.getParameter("idcb");
			String noidung = request.getParameter("noidung");
			String idchuyenan = request.getParameter("idchuyenan");
			String ngaytiepnhan = request.getParameter("ngaytiepnhan");
			String ngaykhoito = request.getParameter("ngaykhoito");
			String sobican = request.getParameter("sobican");
			String tiendoDT = request.getParameter("tiendoDT");

			String thoihanDT = request.getParameter("thoihanDT");
			String thoihanDT1 = thoihanDT.substring(0, 10);
			String thoihanDT2 = thoihanDT.substring(13, 23);
			String thoihanTG = request.getParameter("thoihanTG");
			String thoihanTG1 = thoihanTG.substring(0, 10);
			String thoihanTG2 = thoihanTG.substring(13, 23);

			int idVuan = vuAnDAO.getIDvuan();

			boolean them = vuAnDAO.themVuan(idVuan, idcb, idchuyenan, tenvuan,
					noidung, ngaykhoito, ngaytiepnhan, sobican, tiendoDT,
					thoihanDT1, thoihanDT2, thoihanTG1, thoihanTG2);
			
			
			ArrayList<Canbo> dsCanBo = canBoDAO.dsCanbo();
			ArrayList<ChuyenAn> dsChuyenAn = chuyenAnDAO.dsChuyenan();
			
			request.setAttribute("dsCanBo", dsCanBo);
			request.setAttribute("dsChuyenAn", dsChuyenAn);
			request.setAttribute("them", them ? "true":"false");
			

			RequestDispatcher rd = request
					.getRequestDispatcher("form_vuan.jsp");
			rd.forward(request, response);

		}
		
		//Cap nhat
		else if (request.getParameter("tenvuan2") != null) {
			
			String idva = request.getParameter("idva");
			String tenvuan = request.getParameter("tenvuan2");
			String idcb = request.getParameter("idcb");
			String noidung = request.getParameter("noidung");
			String idchuyenan = request.getParameter("idchuyenan");
			String ngaytiepnhan = request.getParameter("ngaytiepnhan");
			String ngaykhoito = request.getParameter("ngaykhoito");
			String sobican = request.getParameter("sobican");
			String tiendoDT = request.getParameter("tiendoDT");

			String thoihanDT = request.getParameter("thoihanDT");
			String thoihanDT1 = thoihanDT.substring(0, 10);
			String thoihanDT2 = thoihanDT.substring(13, 23);
			String thoihanTG = request.getParameter("thoihanTG");
			String thoihanTG1 = thoihanTG.substring(0, 10);
			String thoihanTG2 = thoihanTG.substring(13, 23);
			
			boolean sua = vuAnDAO.suaVuan(idva, idcb, idchuyenan, tenvuan,
					noidung, ngaykhoito, ngaytiepnhan, sobican, tiendoDT,
					thoihanDT1, thoihanDT2, thoihanTG1, thoihanTG2);
			request.setAttribute("sua", sua ? "true":"false");
			
			
			VuAn vuan = vuAnDAO.getVuan(idva);
			request.setAttribute("vuan", vuan);
			
			ArrayList<Canbo> dsCanBo = canBoDAO.dsCanbo();
			ArrayList<ChuyenAn> dsChuyenAn = chuyenAnDAO.dsChuyenan();
			
			request.setAttribute("dsCanBo", dsCanBo);
			request.setAttribute("dsChuyenAn", dsChuyenAn);

			RequestDispatcher rd = request
					.getRequestDispatcher("sua_vuan.jsp");
			rd.forward(request, response);

		}
		
		
		else if(request.getParameter("xoa") != null) {
			
			String idva = request.getParameter("xoa");
			
			boolean xoa = vuAnDAO.xoaVuAn(idva);
			request.setAttribute("xoa", xoa ?"true":"false");
			
			ArrayList<VuAn> dsVuAn = vuAnDAO.dsVuAn();
			request.setAttribute("dsVuAn", dsVuAn);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_vuan.jsp");
			rd.forward(request, response);
		}
		
		else if(request.getParameter("sua") != null) {
			String idva = request.getParameter("idva");
			
			VuAn vuan = vuAnDAO.getVuan(idva);
			request.setAttribute("vuan", vuan);
			
			ArrayList<Canbo> dsCanBo = canBoDAO.dsCanbo();
			ArrayList<ChuyenAn> dsChuyenAn = chuyenAnDAO.dsChuyenan();
			
			request.setAttribute("dsCanBo", dsCanBo);
			request.setAttribute("dsChuyenAn", dsChuyenAn);

			RequestDispatcher rd = request
					.getRequestDispatcher("sua_vuan.jsp");
			rd.forward(request, response);
			
		}
		
		if(request.getParameter("dsVuAn") != null) {
			
			ArrayList<VuAn> dsVuAn = vuAnDAO.dsVuAn();
			request.setAttribute("dsVuAn", dsVuAn);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_vuan.jsp");
			rd.forward(request, response);
		}
	}

}
